﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolutionModel;
using SFast.DB;
using System.Linq.Expressions;
using SolutionDAL;
using SFast;


namespace SolutionBLL
{
    public class T_UsersInRolesBLL
    {

        private T_UsersInRolesDAL dal;

        public T_UsersInRolesDAL Dal
        {
            get
            {
                if (dal == null)
                {
                    dal = new T_UsersInRolesDAL();
                }
                return dal;
            }
            set { dal = value; }
        }

        #region 通过用户ID获取该用户的所有角色信息
        /// <summary>
        /// 通过用户ID获取该用户的所有角色信息
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <returns></returns>
        public List<T_AdminRole> GetRolesByUser(String userid)
        {
            return Dal.GetRolesByUser(userid);
        }
        #endregion

        #region 通过角色ID获取该角色下的所有用户
        /// <summary>
        /// 通过角色ID获取该角色下的所有用户
        /// </summary>
        /// <param name="roleid">角色ID</param>
        /// <param name="userNameKey">用户名、用户姓名关键词</param>
        /// <returns></returns>
        public List<T_AdminUser> getUserByRoleID(String roleid, String userNameKey)
        {
            IQueryable<T_AdminUser> users = Dal.getUserByRoleID(roleid, userNameKey);
            return users.ToList();
        }
        #endregion

        #region 添加或更新管理员的所属角色(webform)
        /// <summary>
        /// 添加或更新管理员的所属角色
        /// 标记：需要修改
        /// </summary>
        /// <param name="userid">当前管理员ID</param>
        /// <param name="list">所属角色的角色ID集合</param>
        public void AddOrUpdate(String userid, List<String> list)
        {
            Dal.AddOrUpdate(userid, list);
        }
        #endregion

        #region 添加用户，给指定用户添加用户
        /// <summary>
        /// 添加用户，给指定用户添加用户
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="userIds">用户ID字符串，用逗号分割</param>
        /// <returns></returns>
        public int addUserForRole(String roleId, String userIds)
        {
            List<T_UsersInRole> list = new List<T_UsersInRole>();
            String[] ids = userIds.Split(',');
            foreach (String item in ids)
            {
                T_UsersInRole uir = new T_UsersInRole();
                uir.RoleID = roleId;
                uir.UserID = item;
                list.Add(uir);
            }
            int r = Dal.addUserForRole(list);
            return r;
        }
        #endregion

        #region 删除用户、角色关系
        /// <summary>
        /// 删除用户、角色关系,
        /// </summary>
        /// <param name="ids">关系ID字符串，用逗号分割</param>
        /// <returns></returns>
        public int deleteUserInRole(String roleid, String ids)
        {
            List<String> list = ids.Split(',').ToList();
            int r = Dal.deleteUserByRoleID(roleid,list);
            return r;
        }
        #endregion
    }
}
